package com.antd.admin.server.controller;

import cn.hutool.core.util.RandomUtil;
import com.antd.admin.api.entity.SysSmsLog;
import com.antd.admin.server.service.ISysSmsLogService;
import com.antd.common.core.constant.AntdConstant;
import com.antd.common.core.util.R;
import com.antd.common.sms.constant.SmsConstant;
import com.antd.common.sms.enums.SmsTemplateEnum;
import com.antd.common.sms.util.SmsSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/sms")
public class SysSmsLogController {
    @Autowired
    private ISysSmsLogService sysSmsLogService;
    @Autowired
    private SmsSendService smsSendService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("/saveSmsLog")
    public R<Object> saveSmsLog(@RequestBody SysSmsLog smsLog){
        return R.ok(sysSmsLogService.saveLog(smsLog));
    }


    @GetMapping("/sendSms/{mobile}")
    public R<Object> sendSms(@PathVariable String mobile){
        String code = RandomUtil.randomNumbers(6);
        redisTemplate.opsForValue().set(AntdConstant.ANTD_SMS_CODE_KEY+mobile,code, 2L, TimeUnit.MINUTES);
        String sendSms = smsSendService.sendSms(mobile, SmsTemplateEnum.LOGIN_TEMPLATE, code);
        if(sendSms.equals(SmsConstant.SUCCESS_CODE)){
            return R.ok("发送成功");
        }
        return R.ok("发送成功");
    }
}
